<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 用户信息管理
*/
class User extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_model');
	}
	//查询列表
	public function index()
	{
		$username = $this->input->get_post('username');
		$email = $this->input->get_post('email');
		$roleid = $this->input->get_post('roleid');
		$start =  intval($this->input->get_post('per_page'));

		$params = array(
			'username' =>$username 
			,'email' =>$email 
			,'roleid' =>$roleid
			,'start' => $start
			,'length'=>$this->page_size);

		$this->userinfoa = array(1,2,3);
		$userinfo = $this->User_model->select_user_page($params);
		
		$userinfo['params'] = $params;
		//分页设置
		$base_url = site_url("c=user&m=index&username=$username&email=$email&roleid=$roleid");
		$userinfo['pagelink'] = $this->create_page_links($base_url,$userinfo['count']);

		$this->show_view('userList',$userinfo);
		
	}

	//编辑
	public function edit()
	{

		$this->form_validation->set_rules('username', '用户名', 'required');
		$this->form_validation->set_rules('password', '密码', 'required');
		$this->form_validation->set_rules('password2', '密码确认', 'required');
		$this->form_validation->set_rules('email', '注册邮箱', 'required');

		$uid = $this->input->get_post('uid');

		$usereditsubmit = $this->input->post('usereditsubmit');
		$resultinfo = $this->User_model->select_user_by_id($uid);
		$resultinfo['method'] ='edit';
		if($usereditsubmit  && $this->form_validation->run())
		{
			$data['username'] = $this->input->get_post('username');
			$data['email'] = $this->input->get_post('email');
			$data['password'] = $password = $this->input->get_post('password');
			$password2 = $this->input->get_post('password2');
			
			$result = $this->User_model->update_user($uid,$data);
			$this->show_message("编辑用户信息成功",site_url("c=user&m=index"));
			
		}
		else
		{		
			$this->show_view('userEdit',$resultinfo);
			
		}
	}
	//新增用户
	public function add()
	{
		$this->form_validation->set_rules('username', '用户名', 'required');
		$this->form_validation->set_rules('password', '密码', 'required');
		$this->form_validation->set_rules('password2', '密码确认', 'required');
		$this->form_validation->set_rules('email', '注册邮箱', 'required');
		
		$usereditsubmit = $this->input->post('usereditsubmit');
		$resultinfo['method'] ='add';
		if($usereditsubmit  && $this->form_validation->run())
		{
			
			$data['username'] = $this->input->get_post('username');
			$data['email'] = $this->input->get_post('email');
			$data['password'] = $password = $this->input->get_post('password');
			$data['regdate'] =time();
			$password2 = $this->input->get_post('password2');
			$result = $this->User_model->insert_user($data);

			$this->show_message("新增用户信息成功",site_url("c=user&m=index"));
			
		}
		else
		{		
			$this->show_view('userEdit',$resultinfo);
			
		}
	}
	//删除用户
	public function delete()
	{
		$uid = $this->input->get_post('uid');
		$result = $this->User_model->delete_user($uid);
		if($result['success'])
		{
			$this->show_message("删除用户信息成功",site_url("c=user&m=index"));
		}
		else
		{
			$this->show_message($result['message'],site_url("c=user&m=index"));
		}
		
	}

	//判断用户名是否唯一，返回json对象
	public function unique()
	{
		$uid = intval($this->input->get_post('uid'));
		$username = $this->input->get_post('username');
		$result['status'] =1;
		$result['success'] = $this->User_model->check_unique($username,$uid);
		$this->load->library('json');
		echo $this->json->encode($result);
	}
}